﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// A base class for skinnable portal modules.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Skinnable")]
    public abstract class SkinnablePortalModuleBase : MLPortalModuleBase, IModuleSettingsProvider, IActionable
    {
        #region control fields

        /// <summary>
        /// The <see cref="PlaceHolder"/> control which will contain the skin.
        /// </summary>
        protected PlaceHolder plcSkin;

        #endregion

        #region fields

        private PortalModuleView skin;

        #endregion

        #region abstract properties

        /// <summary>
        /// Gets the default skin url.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
        protected abstract string DefaultSkinUrl { get; }

        /// <summary>
        /// Gets the specific skin url.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
        protected abstract string SkinUrl { get; }

        #endregion

        #region properties

        /// <summary>
        /// Gets whether the required child controls have been asserted.
        /// </summary>
        protected bool Asserted { get; private set; }

        /// <summary>
        /// Gets the currently used skin object.
        /// </summary>
        protected PortalModuleView Skin
        {
            get { return this.skin; }
        }

        #endregion

        #region methods

        /// <summary>
        /// Loads and initializes the module's skin.
        /// </summary>
        protected void InitializeSkin()
        {
            string url = this.MakeSkinUrl(this.SkinUrl);
            if (!File.Exists(this.Server.MapPath(url)))
            {
                url = this.MakeSkinUrl(this.DefaultSkinUrl);
            }

            this.skin = (PortalModuleView)this.LoadControl(url);
            this.skin.ID = "ModuleSkin";
            this.skin.ModuleBase = this;
        }

        /// <summary>
        /// Occurs when the control is initialized.
        /// </summary>
        /// <param name="e">The arguments passed to the event.</param>
        protected override void OnInit(EventArgs e)
        {
            this.AssertRequiredControls();

            this.InitializeSkin();

            this.plcSkin.Controls.Clear();
            this.plcSkin.Controls.Add(this.skin);

            this.skin.DataBind();

            base.OnInit(e);
        }

        /// <summary>
        /// Asserts if all required child controls have been set.
        /// </summary>
        protected virtual void AssertRequiredControls()
        {
            if (this.Asserted)
                return;

            if (null == this.plcSkin)
            {
                throw new NotImplementedException("PlaceHolder 'plcSkin' is not supplied by the user control.");
            }
            this.Asserted = true;
        }

        #endregion

        #region IActionable

        /// <summary>
        /// Retrieves the modules actions.
        /// </summary>
        public ModuleActionCollection ModuleActions
        {
            get
            {
                IActionable actionable = this.skin as IActionable;
                return null != actionable ? actionable.ModuleActions : new ModuleActionCollection();
            }
        }

        #endregion
    }
}